package com.example.wto.viewpagertest01;

import android.os.Environment;
import android.util.Log;

import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ParseTeacherHtml implements ParseHtml
{
	private String cookie = "";
	private CollegeBean collegeBean=null;
	private String college_url;
	private Map<String, TeacherBean> teacherMap=new HashMap<String, TeacherBean>();
	private Map<String, TermBean> termMap=new HashMap<String, TermBean>();

	public static int CODE_ERROR;

	public ParseTeacherHtml(CollegeBean collegeBean)
	{
		super();
		this.collegeBean = collegeBean;
		college_url=collegeBean.getCollege_url();
	}
	public Teacher getTeacher(String tid,String teaid,String code)
	{
		Map<Integer, List<Course>> cmap = sendPost(tid, teaid, code);

		int j=0;
		TeacherBean bean=new TeacherBean(collegeBean.getCollege_id(),collegeBean.getCollege_name(),tid,termMap.get(tid).getTermName(),teaid,teacherMap.get(teaid).getTeacherName());
		Teacher teacher =new Teacher(bean);
		teacher.getWeekList().clear();
		if (cmap != null)
		{
			for (Map.Entry<Integer, List<Course>> l : cmap.entrySet()) {
				System.out.println(WEEK[j]);
				System.out.println(l.getValue());
				if(j>=7)
				{
					break;
				}
				DayOfWeek weekDay=new DayOfWeek(WEEK[j]);
				weekDay.setCourseList(l.getValue());
				teacher.getWeekList().add(weekDay);
				System.out.println(weekDay.toString());
				j++;
			}

			return teacher;
		}
		else
		{
			return null;
		}
	}
	@Override
	public List getIndex()
	{
		try {
			URL url = new URL("http://192.168.20.74:8080/sysedu/teacherPhone?type=getTermList");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			InputStream in = conn.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			StringBuilder builder = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
			String info = builder.toString();
			List<TermBean> termList=ParseJsonUtil.parseFromTermJson(builder.toString());
			if(termMap!=null)
			{
				termMap.clear();
			}
			for (TermBean bean: termList)
			{
				termMap.put(bean.getTermID(), bean);
			}

			return termList;

		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			Log.i("ttttttttt","llllllllllllllll");
			e.printStackTrace();
		}
		return null;
	}

	// ???????? ????? ??? ?????????
	public Map<Integer, List<Course>> sendPost(String termId, String tid, String code)
	{

		Map<Integer, List<Course>> cmap = null;
		try
		{
			URL url = new URL("http://192.168.20.74:8080/sysedu/teacherPhone?type=getCourse&s_id="+termId+"&t_id="+tid+"&checkCode="+code);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			InputStream in = conn.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			StringBuilder builder = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
			String info = builder.toString();
			System.out.println(info);
			if(info.contains("验证码错误"))
			{
				CODE_ERROR=1;
				return null;
			}

			Teacher teacher=ParseJsonUtil.ParseFromTeacherJson(builder.toString());
			cmap = new LinkedHashMap<>();
			if(teacher.getWeekList().size()==0)
			{

				for(int j=0;j<7;j++)
				{
					List<Course> list=new ArrayList<>();
					for(int i=0;i<5;i++)
					{

						list.add(new Course(""));

					}
					cmap.put(j,list);
				}

			}
			else
			{
				for(int i=0;i<teacher.getWeekList().size();i++)
				{
					cmap.put(i, teacher.getWeekList().get(i).getCourseList());
				}
			}

			CODE_ERROR=0;
			return cmap;

		} catch (IOException e)
		{
			e.printStackTrace();
		}
		CODE_ERROR=0;
		return null;

	}

	public int getImage() {
		//
		FileOutputStream fo=null;
		InputStream in=null;
		HttpURLConnection conn=null;
		try {
			//第一次请求图片路径
			URL url = new URL("http://192.168.20.74:8080/sysedu/teacherPhone?type=getCheckCode");
			conn = (HttpURLConnection) url.openConnection();
			File file = new File(Environment.getExternalStorageDirectory()+File.separator+"codeImage.jpg");
			fo = new FileOutputStream(file);

			in = conn.getInputStream();
			byte bytes[]=new byte[1024];
			int num;
			while((num=in.read(bytes,0,bytes.length))!=-1)
			{
				fo.write(bytes,0,num);
			}
			return 1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		finally
		{
			try
			{
				in.close();
				fo.close();
				conn.disconnect();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return -1;
	}



	// ?????????
	public List<TeacherBean> getTeachers(String term)
	{
		try {
			URL url = new URL("http://192.168.20.74:8080/sysedu/teacherPhone?type=getTeacherList");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			InputStream in = conn.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			StringBuilder builder = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
			String info = builder.toString();
			List<TeacherBean> termList=ParseJsonUtil.parseFromTeacherListJson(builder.toString());
			if(teacherMap!=null)
			{
				teacherMap.clear();
			}
			for (TeacherBean bean: termList)
			{
				teacherMap.put(bean.getTeacherID(), bean);
			}

			return termList;

		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			Log.i("ttttttttt","llllllllllllllll");
			e.printStackTrace();
		}
		return null;
	}

	public static String string2Unicode(String string) {

		StringBuffer unicode = new StringBuffer();

		for (int i = 0; i < string.length(); i++) {

			// ??????????
			char c = string.charAt(i);

			// ????unicode
			unicode.append("\\u" + Integer.toHexString(c));
		}

		return unicode.toString();
	}

	public String getCookie()
	{
		return cookie;
	}

	public void setCookie(String cookie)
	{
		this.cookie = cookie;
	}

	public CollegeBean getCollegeBean()
	{
		return collegeBean;
	}

	public void setCollegeBean(CollegeBean collegeBean)
	{
		this.collegeBean = collegeBean;
	}

	@Override
	public Object get(String sid, String id, String code)
	{
		return getTeacher(sid, id, code);
	}
}
